package edu.utsa.cs.ce.image;

import java.io.File;
import java.io.IOException;

import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;

import edu.utsa.cs.vignette.annotations.desc;

public class Trim {
	
	@desc("Returns a File representing a trimmed image given an untrimmed File image")
	public static File trim(File inFile) {
		File outFile;
		try {
			outFile = File.createTempFile("TRM", "TRM");
			trim(inFile, outFile);
			return outFile;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void trim(File inFile, File outFile) {
		ConvertCmd cmd = new ConvertCmd();
		
		IMOperation op = new IMOperation();
		
		op.addImage(inFile.getAbsolutePath());
		op.trim();
		op.addImage(outFile.getAbsolutePath());
		try {
			cmd.run(op);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (IM4JavaException e) {
			e.printStackTrace();
		}
	}

}
